package com.tpv.tv.lib.pdf;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class PdfPageBottomBarSelectedHelper {
    public static final int NONE_RES_ID = -1;
    private final List<SelectedItem> btnStartSelectedState = new ArrayList<>(6);
    private INotifyIdResSelected notifyIdResSelected;

    public static PdfPageBottomBarSelectedHelper create() {
        return new PdfPageBottomBarSelectedHelper();
    }

    public PdfPageBottomBarSelectedHelper setNotifyIdResSelected(INotifyIdResSelected notifyIdResSelected) {
        this.notifyIdResSelected = notifyIdResSelected;
        return this;
    }

    /** 排序需要跟布局一致 **/
    public PdfPageBottomBarSelectedHelper() {
        btnStartSelectedState.add(new SelectedItem(R.id.btn_suitable_page, false));
        btnStartSelectedState.add(new SelectedItem(R.id.btn_pdf_page_start, false));
        btnStartSelectedState.add(new SelectedItem(R.id.btn_pdf_page_previous, false));
        btnStartSelectedState.add(new SelectedItem(R.id.btn_pdf_page_next, false));
        btnStartSelectedState.add(new SelectedItem(R.id.btn_pdf_page_end, false));
        btnStartSelectedState.add(new SelectedItem(R.id.btn_turn_page_auto, false));
    }

    public void updateBtnSelectedState(int idRes, boolean selected, boolean isEnable) {
        logResId("updateBtnSelectedState", idRes);
        PdfLogUtil.logD("[updateBtnSelectedState] selected : " + selected);
        SelectedItem item = new SelectedItem(idRes, selected);
        item.setEnable(isEnable);
        int index = btnStartSelectedState.indexOf(item);
        if(index != -1) btnStartSelectedState.set(index, item);
    }

    public static void logResId(String tag, int res) {
        if(R.id.btn_suitable_page == res) {
            PdfLogUtil.logD("[" + tag + "] btn_suitable_page");
        } else if(R.id.btn_pdf_page_start == res) {
            PdfLogUtil.logD("[" + tag + "] btn_pdf_page_start");
        } else if(R.id.btn_pdf_page_end == res) {
            PdfLogUtil.logD("[" + tag + "] btn_pdf_page_end");
        } else if(R.id.btn_pdf_page_previous == res) {
            PdfLogUtil.logD("[" + tag + "] btn_pdf_page_previous");
        } else if(R.id.btn_pdf_page_next == res) {
            PdfLogUtil.logD("[" + tag + "] btn_pdf_page_next");
        } else if(R.id.btn_turn_page_auto == res) {
            PdfLogUtil.logD("[" + tag + "] btn_turn_page_auto");
        }
    }

    public void findWhichOneSelectedThenUnSelected() {
        for (int i = 0; i < btnStartSelectedState.size(); i++) {
            SelectedItem item = btnStartSelectedState.get(i);
            boolean selected = item.isSelected();
            if (selected) {
                btnStartSelectedState.set(i, new SelectedItem(item.getIdRes(), false));
            }
        }
    }

    public int findWhichOneSelectedIdResSimple() {
        for (int i = 0; i < btnStartSelectedState.size(); i++) {
            SelectedItem item = btnStartSelectedState.get(i);
            boolean selected = item.isSelected();
            if(selected) {
                return item.getIdRes();
            }
        }
        return NONE_RES_ID;
    }

    public SelectedIdResWithBothSide findWhichOneSelectedIdRes() {
        SelectedIdResWithBothSide idsObj = new SelectedIdResWithBothSide();
        for (int i = 0; i < btnStartSelectedState.size(); i++) {
            SelectedItem item = btnStartSelectedState.get(i);
            boolean selected = item.isSelected();
            if(selected) {
                idsObj.selectedIdRes = item.getIdRes();
                if(i == 0) {
                    idsObj.leftSideSelectedIdRes = NONE_RES_ID;
                    idsObj.rightSideSelectedIdRes = findItemEnable(i, false);
                } else if(i == btnStartSelectedState.size() - 1) {
                    idsObj.leftSideSelectedIdRes = findItemEnable(i, true);
                    idsObj.rightSideSelectedIdRes = NONE_RES_ID;
                } else {
                    idsObj.leftSideSelectedIdRes = findItemEnable(i, true);
                    idsObj.rightSideSelectedIdRes = findItemEnable(i, false);
                }
                PdfLogUtil.logD("[findWhichOneSelectedIdRes] idsObj : " + idsObj);
                return idsObj;
            }
        }
        idsObj.selectedIdRes = NONE_RES_ID;
        idsObj.leftSideSelectedIdRes = NONE_RES_ID;
        idsObj.rightSideSelectedIdRes = NONE_RES_ID;
        PdfLogUtil.logD("[findWhichOneSelectedIdRes] idsObj : " + idsObj);
        return idsObj;
    }

    private int findItemEnable(int index, boolean isLeft) {
        if(isLeft) {
            for (int i = index - 1; i >= 0; i--) {
                SelectedItem item = btnStartSelectedState.get(i);
                if(item.isEnable()) {
                    return item.getIdRes();
                }
            }
        } else {
            for (int i = index + 1; i < btnStartSelectedState.size(); i++) {
                SelectedItem item = btnStartSelectedState.get(i);
                if(item.isEnable()) {
                    return item.getIdRes();
                }
            }
        }
        return NONE_RES_ID;
    }

    public void updateItemEnable(int resId, boolean enable) {
        int index = btnStartSelectedState.indexOf(new SelectedItem(resId, false));
        if(index != -1) {
            SelectedItem item = btnStartSelectedState.get(index);
            item.setEnable(enable);
            btnStartSelectedState.set(index, item);
        }
        printBtnStartSelectedState();
    }

    private void printBtnStartSelectedState() {
        btnStartSelectedState.forEach(new Consumer<SelectedItem>() {
            @Override
            public void accept(SelectedItem selectedItem) {
                PdfLogUtil.logD(selectedItem.toString());
            }
        });
    }

    public void notifyIdResSelected(int idRes, boolean needSelected) {
        if(notifyIdResSelected != null) {
            notifyIdResSelected.onNotifyIdResSelected(idRes, needSelected);
        }
    }

    public interface INotifyIdResSelected {
        void onNotifyIdResSelected(int res, boolean needSelected);
    }

    public static class SelectedIdResWithBothSide {
        private int selectedIdRes;
        private int leftSideSelectedIdRes;
        private int rightSideSelectedIdRes;

        public int getSelectedIdRes() {
            return selectedIdRes;
        }

        public void setSelectedIdRes(int selectedIdRes) {
            this.selectedIdRes = selectedIdRes;
        }

        public int getLeftSideSelectedIdRes() {
            return leftSideSelectedIdRes;
        }

        public void setLeftSideSelectedIdRes(int leftSideSelectedIdRes) {
            this.leftSideSelectedIdRes = leftSideSelectedIdRes;
        }

        public int getRightSideSelectedIdRes() {
            return rightSideSelectedIdRes;
        }

        public void setRightSideSelectedIdRes(int rightSideSelectedIdRes) {
            this.rightSideSelectedIdRes = rightSideSelectedIdRes;
        }

        @NonNull
        @Override
        public String toString() {
            logResId("selectedIdRes", selectedIdRes);
            logResId("leftSideSelectedIdRes", leftSideSelectedIdRes);
            logResId("rightSideSelectedIdRes", rightSideSelectedIdRes);
            return "SelectedIdResWithBothSide{" +
                    "selectedIdRes=" + selectedIdRes +
                    ", leftSideSelectedIdRes=" + leftSideSelectedIdRes +
                    ", rightSideSelectedIdRes=" + rightSideSelectedIdRes +
                    '}';
        }
    }

    public static class SelectedItem {
        private int idRes;
        private boolean isSelected;
        private boolean isEnable = true;

        public SelectedItem(int idRes, boolean isSelected) {
            this.idRes = idRes;
            this.isSelected = isSelected;
        }

        public int getIdRes() {
            return idRes;
        }

        public void setIdRes(int idRes) {
            this.idRes = idRes;
        }

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        public boolean isEnable() {
            return isEnable;
        }

        public void setEnable(boolean enable) {
            isEnable = enable;
        }

        @Override
        public boolean equals(@Nullable Object obj) {
            if(!(obj instanceof SelectedItem)) return false;
            SelectedItem item = (SelectedItem) obj;
            return this.idRes == item.idRes;
        }

        @NonNull
        @Override
        public String toString() {
            logResId("SelectedItem", idRes);
            return "SelectedItem{" + "isSelected=" + isSelected +
                    ", isEnable=" + isEnable +
                    '}';
        }
    }
}
